#if 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MINISQL_IMPLEMENTATION
#include "minisql.hol.h"

// 生成随机字符串
void random_string(char* buf, int len) {
    const char* chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for (int i = 0; i < len; i++) {
        buf[i] = chars[rand() % strlen(chars)];
    }
    buf[len] = '\0';
}

// 测试插入性能
void test_insert_performance(MiniSQL* db, const char* table_name, int count) {
    printf("测试插入 %d 条记录...\n", count);
    
    clock_t start = clock();
    
    // 开始事务以提高性能
    minisql_exec(db, "BEGIN", NULL);
    
    char sql[1024];
    char str[32];
    
    for (int i = 0; i < count; i++) {
        random_string(str, 10);
        snprintf(sql, sizeof(sql), 
                 "INSERT INTO %s VALUES (%d, '%s', %d)",
                 table_name, i + 1000, str, rand() % 100);
        
        ResultSet* result = NULL;
        if (minisql_exec(db, sql, &result) != MINISQL_OK) {
            printf("插入失败: %s\n", minisql_errmsg(db));
            minisql_exec(db, "ROLLBACK", NULL);
            return;
        }
        if (result) minisql_free_result(result);
    }
    
    // 提交事务
    minisql_exec(db, "COMMIT", NULL);
    
    clock_t end = clock();
    double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
    
    printf("插入完成: %d 条记录，耗时 %.2f 秒，平均每秒 %.2f 条\n",
           count, time_spent, count / time_spent);
}

// 测试查询性能
void test_query_performance(MiniSQL* db, const char* table_name, int count) {
    printf("测试查询 %d 次...\n", count);
    
    clock_t start = clock();
    
    char sql[1024];
    
    for (int i = 0; i < count; i++) {
        // 随机查询条件
        int age = rand() % 100;
        snprintf(sql, sizeof(sql), 
                 "SELECT * FROM %s WHERE age = %d",
                 table_name, age);
        
        ResultSet* result = NULL;
        if (minisql_exec(db, sql, &result) != MINISQL_OK) {
            printf("查询失败: %s\n", minisql_errmsg(db));
            return;
        }
        
        if (result) {
            // 这里只是为了测试，实际应用中应该处理结果
            minisql_free_result(result);
        }
    }
    
    clock_t end = clock();
    double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
    
    printf("查询完成: %d 次查询，耗时 %.2f 秒，平均每秒 %.2f 次\n",
           count, time_spent, count / time_spent);
}

int main(int argc, char* argv[]) {
    srand(time(NULL));
    
    // 创建测试数据库
    MiniSQL* db = minisql_open("performance_test");
    if (!db) {
        printf("无法打开数据库\n");
        return 1;
    }
    
    // 创建测试表
    const char* create_sql = "CREATE TABLE test ("
                            "id INT PRIMARY KEY,"
                            "name TEXT,"
                            "age INT)";
    ResultSet* result = NULL;
    minisql_exec(db, create_sql, &result);
    if (result) minisql_free_result(result);
    
    // 创建索引
    minisql_exec(db, "CREATE INDEX idx_age ON test(age)", NULL);
    
    // 执行性能测试
    test_insert_performance(db, "test", 1000);
    test_query_performance(db, "test", 500);
    
    // 清理测试数据
    minisql_exec(db, "DROP TABLE test", NULL);
    
    minisql_close(db);
    return 0;
}
#endif 